// var exec = require("child_process").exec;
var querystring = require("querystring"), fs = require("fs");
var formidable = require("formidable");

function start(res) {
    console.log(new Date().getTime() + ":Request handler 'start' was called.");

    // exec("ls -lah", function(error, stdout, stderr) {
    //     res.writeHead(200, {"Content-Type": "text/plain"});
    //     res.write(stdout);
    //     res.end;
    // });

    var body = '<html>' +
        '<head>' +
        '<meta http-equiv="Content-Type" ' +
        'content="text/html; charset=UTF-8" />' +
        '</head>' +
        '<body>' +
        '<form action="/upload" enctype="multipart/form-data" ' +
        'method="post">' +
        '<input type="file" name="upload">' +
        '<input type="submit" value="Upload file" />' +
        '</form>' +
        '</body>' +
        '</html>';

    res.writeHead(200, { "Content-Type": "text/html" });
    res.write(body);
    res.end();
}

function upload(res, req) {

    console.log("Request handler 'upload' was called.");
    var form =new formidable.IncomingForm();
    form.parse(req, function(error, fields, files) {
        console.log('upload path:', files.upload.path);
        fs.renameSync(files.upload.path, "/Users/dengxin/git/wendy2.jpeg");
        res.writeHead(200, { "Content-Type": "text/html" });
        res.write("received image:<br/>");
        res.write("<img src='/show' />");
        res.end();
    });

}

function show(res) {
    console.log("Request handler 'show' was called.");
    fs.readFile("/Users/dengxin/git/wendy2.jpeg", "binary", function (error, file) {
        if (error) {
            res.writeHead(500, { "Content-Type": "text/plain" });
            res.write(error + "\n");
            res.end();
        } else {
            res.writeHead(200, { "Content-Type": "image/png" });
            res.write(file, "binary");
            res.end();
        }
    })
}

exports.start = start;
exports.upload = upload;
exports.show = show;